import { WangEditor } from '@/components'
import { cyyUploadImg } from '@/components/CyyUi/index'
import productList from './productlist'
import userList from './userlist'
import { poolDetail, savePoolInfo } from '@/api/plat'

export default {
    name: 'pool-list-add-or-edit',
    components: {
        WangEditor,
        cyyUploadImg,
        productList,
        userList
    },
    data() {
        return {
            dialogTile: "添加分红池",
            dialogVisible: false,
            loading: false,
            labelCol: { span: 4 },
            wrapperCol: { span: 20 },
            formValidated: {
                id: null,
                name: "",
                ico: "",
                detail: "",
                condition: {
                    self_buy_goods: { is_open: 1, start: 0, end: 0, goods_ids: null },
                    spread_buy_goods: { is_open: 1, spread_num: 0, goods_ids: null },
                    cycle_buy_goods: { is_open: 1, spread_num: 0, goods_ids: null },
                    specify_user: { is_open: 1, user_ids: null },
                    see_ad_num: { is_open: 1, num: 0 }
                },
                rate: "",
                cycle_type: 1,
                status: 1,
                is_display: 1,
                condition_type: 2
            },
            desc: '',
            rules: {
                name: [{ required: true, message: '请输入分红池名称', trigger: 'blur' }],
                rate: [{ required: true, message: '请填写分红池比例', trigger: 'blur' }],
                ico: [{ required: true, message: '请上传分红池图标', trigger: 'change' }],
                // detail: [{ required: true, message: '请填写分红池说明', trigger: 'change' }],
                condition_type: [{ required: true, message: '请选择达成条件类型', trigger: 'change' }],
                condition: [{ required: true, message: '请填写达成条件', trigger: 'change' }],
                cycle_type: [{ required: true, message: '请选择分红周期', trigger: 'change' }],
                status: [{ required: true, message: '请选择是否禁用', trigger: 'change' }],
                is_display: [{ required: true, message: '请选择前端显示', trigger: 'change' }]
            },
        }
    },

    created() { },
    methods: {
        handleShow(row) {
            this.reset()
            if (row) {
                this.formValidated.id = row.id;
                this.getDetail(); // 获取详情
                this.dialogTile = "编辑分红池";
                this.dialogVisible = true;
            } else {
                this.formValidated.id = null;
                this.dialogVisible = true;
            }
        },
        handleCancel() {
            this.reset();
            this.dialogVisible = false;
        },
        getDetail() {
            poolDetail(this.formValidated.id)
                .then(res => {
                    this.formValidated.name = res.data.info.name || "";
                    this.formValidated.ico = res.data.info.ico || "";
                    this.formValidated.detail = res.data.info.detail || "";
                    this.formValidated.condition_type = +res.data.info.condition_type || 2;
                    this.formValidated.condition = res.data.info.condition;
                    this.formValidated.rate = res.data.info.rate || "";
                    this.formValidated.cycle_type = +res.data.info.cycle_type || 1;
                    this.formValidated.status = +res.data.info.status || 1;
                    this.formValidated.is_display = +res.data.info.is_display || 1;

                    this.desc = res.data.info.detail || "";
                })
                .catch(err => {
                    this.$message.error(err)
                })
        },
        descChange(content) {
            this.formValidated.detail = content
        },
        selectProductItem(name) {
            if (this.formValidated.condition[name].goods_ids) {
                this.$nextTick(() => {
                    this.$refs.productList.handleShow(name, this.formValidated.condition[name].goods_ids, true)
                })
            } else {
                this.$nextTick(() => {
                    this.$refs.productList.handleShow(name, null, true)
                })
            }
        },
        selectUsers(name) {
            if (this.formValidated.condition[name].user_ids) {
                this.$nextTick(() => {
                    this.$refs.userList.handleShow(name, this.formValidated.condition[name].user_ids, true)
                })
            } else {
                this.$nextTick(() => {
                    this.$refs.userList.handleShow(name, null, true)
                })
            }
        },
        getGoodsIds(name, goods_ids) {
            this.formValidated.condition[name].goods_ids = goods_ids;
        },
        getUserIds(name, user_ids) {
            console.log(123, name, user_ids)
            this.formValidated.condition[name].user_ids = user_ids;
            console.log(456, this.formValidated.condition)
        },
        confirmBtn(name) {
            this.$refs[name].validate(valid => {
                if (valid) {
                    this.loading = true;
                    savePoolInfo(this.formValidated)
                        .then(res => {
                            this.loading = false;
                            this.$emit("getList")
                            this.reset();
                            this.dialogVisible = false;
                            this.$message.success(res.msg)
                        })
                        .catch(err => {
                            this.loading = false;
                            this.$message.error(err)
                        })
                } else {
                    return false;
                }
            });
        },
        reset() {
            this.formValidated = {
                id: null,
                name: "",
                ico: "",
                detail: "",
                condition: {
                    self_buy_goods: { is_open: 1, start: 0, end: 0, goods_ids: null },
                    spread_buy_goods: { is_open: 1, spread_num: 0, goods_ids: null },
                    cycle_buy_goods: { is_open: 1, spread_num: 0, goods_ids: null },
                    specify_user: { is_open: 1, user_ids: null },
                    see_ad_num: { is_open: 1, num: 0 }
                },
                rate: "",
                cycle_type: 1,
                status: 1,
                is_display: 1,
                condition_type: 2
            };
            this.desc = "";
            // this.$refs.ruleFormValidated.resetFields();
        },
    },
}